package com.ruoyi.project.lease.car.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lease.car.domain.Car;
import com.ruoyi.project.lease.car.service.ICarService;
import com.ruoyi.project.lease.customer.domain.Customer;
import com.ruoyi.project.lease.customer.service.ICustomerService;
import com.ruoyi.project.lease.order.domain.Order;
import com.ruoyi.project.lease.order.service.IOrderService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 客户的车辆管理Controller
 *
 * @author admin
 * @date 2022-04-28
 */
@Controller
@RequestMapping("/lease/custcar")
public class CustCarController extends BaseController {

    private String prefix = "lease/car";

    @Autowired
    private ICarService carService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IOrderService orderService;

    @RequiresPermissions("lease:custcar:view")
    @RequiresRoles("customer")
    @GetMapping()
    public String car()
    {
        return prefix + "/cust_car";
    }

    /**
     * 查询车辆管理列表
     */
    @RequiresPermissions("lease:custcar:list")
    @RequiresRoles("customer")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Car car)
    {
        startPage();
        car.setDelFlag(0);
        List<Car> list = carService.selectCarList(car);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @RequiresPermissions("lease:custcar:export")
    @RequiresRoles("customer")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Car car)
    {
        car.setDelFlag(0);
        List<Car> list = carService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        return util.exportExcel(list, "车辆管理数据");
    }


    /**
     * 预览车辆信息
     */
    @RequiresPermissions("lease:custcar:view")
    @RequiresRoles("customer")
    @GetMapping( "/preview/{carId}")
    public String preview(@PathVariable("carId") long id, ModelMap mmap) {
        Car car = carService.selectCarByCarIdDelFlag(id,0);
        mmap.put("car", car);
        return prefix + "/preview";
    }

    /**
     * 点击出租车辆
     */
    @RequiresPermissions("lease:custcar:edit")
    @RequiresRoles("customer")
    @GetMapping("/rent/{carId}")
    public String rent(@PathVariable("carId") Long carId, ModelMap mmap)
    {
        Car car = carService.selectCarByCarIdDelFlag(carId, 0);

        // 获取当前的用户
        User currentUser = ShiroUtils.getSysUser();
        Customer currentCustomer = customerService.selectCustomerByCustPhone(currentUser.getLoginName());
        mmap.put("car", car);
        mmap.put("customer", currentCustomer);
        return prefix + "/cust_rent";
    }

    /**
     * 保存出租车辆
     */
    @RequiresPermissions("lease:custcar:edit")
    @RequiresRoles("customer")
    @Log(title = "车辆管理", businessType = BusinessType.RENT)
    @PostMapping("/rent")
    @ResponseBody
    public AjaxResult rentSave(@RequestParam(value = "carId") Long carId,
                               @RequestParam(value = "custNo") Long custNo,
                               @RequestParam(value = "rent_datetime") Date rent_datetime,
                               @RequestParam(value = "return_datetime") Date return_datetime)
    {
        Car car = new Car();
        car.setCarId(carId);
        car.setCustNo(custNo);
        // 车辆状态设置为待确认
        car.setCarStatus(3);
        Order order = new Order();
        order.setCustNo(custNo);
        order.setCarId(carId);
        order.setRentTime(rent_datetime);
        order.setExpectReturnTime(return_datetime);
        orderService.insertOrder(order);
        return toAjax(carService.updateCar(car));
    }

}
